<script type="text/javascript">

var paginationTotal = 0;//记录总数

function showBindWindow(){
    showEditPage($('#bind'),'<{$lang.bind}><{$lang.shipping_method}>','add');
	searchCustomer();
}

$(function(){
//	initData(0); //初始化分页<{$lang.data}>
	
	$("#bind").dialog({
		autoOpen:false,
		width:800,
		height:500,
		modal:true,
		show:"slide",
        zIndex:100
	});
    $(".messageDiv").dialog({
		autoOpen:false,
		width:800,
		height:200,
		modal:true,
		show:"slide",
        zIndex:200,
        title:"<{$lang.tip}><{$lang.msg}>",
        buttons:{
            "<{$lang.confirm}>": function() {$(this).dialog('close');},
            "<{$lang.close}>": function() {$(this).dialog('close');}
        }
	});

    $("#confrimDiv").dialog({
		autoOpen:false,
		width:800,
		height:200,
		modal:true,
		show:"slide",
        zIndex:200,
        title:"<{$lang.tip}><{$lang.msg}>"
	});
});

function loadData(page,pageSize){
    var st_code = $('[name=st_code]').val();
    var customer_id = $('[name=customer_id]').val();
	$.ajax({ 
		type: "POST",
		async: false,
		dataType: "json",
		url: "/shipping/ship-type-customer/index", 
		data:{
			'page':page,
			'pageSize':pageSize,
            'st_code':st_code,
			'customer_id':customer_id
		},
		success: function(json) { 
			var html = "";
			if(json.count ==0) {
			    paginationTotal = 1;
				html = "<td colspan='8' class=''>&nbsp;<{$lang.no_data}></td>";
			}else {
                paginationTotal = json.count;
                $.each(json.list,function(key,item){
                    html += "<tr>";
					html += "<td><input type='checkbox' value='" + item.stc_id + "' id='stc_id_" + item.stc_id + "' name='stc_id[]' class='stc_id0'></td>";
					html += "<td>"+item.stc_id+"</td>";
					html += "<td>"+item.st_code+"</td>";					
					html += "<td>"+item.st_name_cn+"</td>";
					if(item.customer_id=="*"){
					     html += "<td>"+'[<{$lang.all}><{$lang.customer}>]'+"</td>";
					}else{
					    html += "<td>"+item.customer_id+"</td>";
					}
                   	var tip="<{$lang.ok}><{$lang.unbind}><{$lang.customer}><{$lang.shipping_method}>?";								
					html += "<td><a href=\"javascript:void(0)\" onclick=\"crmDelTip('"+tip+"',"+item.stc_id+")\"><img src='/images/icon/delete.gif' title='<{$lang.delete}>'></a></td>";
					html += "</tr>";
                })
			}
			$("#list").html(html);
			
		}
	}); 
}

function search() {
   initData(0); 
}

function showEditPage(obj,titles,asc){
    if(asc =='add'){
        obj.dialog({
                title:titles,
                buttons:{
                    '<{$lang.bind}>': function() {
					bindSubmit()
					},
                    '<{$lang.close}>': function() {$(
					this).dialog('close');
					}
                }
        });
    }else{
        obj.dialog({
                title:titles,
                buttons:{
                    '<{$lang.edit}>': function() {editSubmit()},
                    '<{$lang.close}>': function() {$(this).dialog('close');}
                }
        });
    }
    obj.dialog("open");
}
function editPage(st_id){
   
}

//<{$lang.operation}><{$lang.tip}>
function crmTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
			  $(this).dialog("close");
               bindAllSubmit();
			   
            },
			'close': function() {
                $(this).dialog("close");
            }
        }
    });
}

//<{$lang.operation}><{$lang.tip}>
function crmMsg(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
			  $(this).dialog("close");
               bindSelectData();
			   
            },
			'close': function() {
                $(this).dialog("close");
            }
        }
    });
}

function bindSubmit(){
    var formData =$('#bindForm').serialize();
	var st_code_bind=$("#st_code_bind").find("option:selected").val();
	var st_bind_type= $("input[name='st_bind_type']:checked").val(); 
	var customerids=  $("input[name='customerid[]']:checked").val();
	if(st_code_bind.length==0){
	   alert("<{$lang.pls}><{$lang.shipping_method}>!");
	   return;
	}
	if(st_bind_type==1){
	    if(!customerids){
	       alert('<{$lang.pls}><{$lang.bind}><{$lang.customer}>!');
		   return;
	    }else{
		   crmMsg('<{$lang.ok}><{$lang.shipping_method}><{$lang.bind}><{$lang.customer}>?');
		   return;
		}
	}else{
	    crmTip('<{$lang.ok}><{$lang.shipping_method}><{$lang.shipping_method}><{$lang.bind}><{$lang.customer}>?');
		return;
	}
   
}

function bindSelectData(){
   var formData =$('#bindForm').serialize();
   $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/shipping/ship-type-customer/bind", //请求的处理url
        data: formData,
        success: function(json){
            var html ="";
            if(json.ask){
                alert('<{$lang.bind}><{$lang.success}>!');
            }else{
                alert('<{$lang.bind}><{$lang.fail}>!');
            }
            
        }
    });
}

function bindAllSubmit(){
    var bindForm =$('#bindForm').serialize();
	var st_code_bind=$("#st_code_bind").find("option:selected").val();
	var st_bind_type= $("input[name='st_bind_type']:checked").val(); 
	var customerids=  $("input[name='customerid[]']:checked").val();
	if(st_code_bind.length==0){
	   alert("<{$lang.pls}><{$lang.shipping_method}>!");
	   return;
	}
	if(st_bind_type==1){
	    if(!customerids){
	       alert('<{$lang.pls}><{$lang.bind}><{$lang.customer}>!');
		   return;
	    }
	}

    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/shipping/ship-type-customer/bind", //请求的处理url
        data: bindForm,
        success: function(json){
            var html ="";
            if(json.ask){
                alert('<{$lang.bind}><{$lang.success}>!');
            }else{
                alert('<{$lang.bind}><{$lang.fail}>!');
            }
            
        }
    });
}

function alertInfo(customerid,st_code){
   $("#confrimDiv").dialog({
		buttons:{
            "<{$lang.confirm}>":function(){del(st_id,st_code)},
            "<{$lang.close}>": function(){$(this).dialog('close')}
        }
	});
   $('#confrimDiv').html("<{$lang.confirm}><{$lang.delete}><{$lang.shipping_method}>"+st_code+'?');
   $('#confrimDiv').dialog("open");
}

//<{$lang.operation}><{$lang.tip}>
function crmDelTip(tip,stc_id) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
			   $(this).dialog("close");
               del(stc_id);
			  
            },
			'close': function() {
                $(this).dialog("close");
            }
        }
    });
}

//<{$lang.operation}><{$lang.tip}>
function crmDelRowsTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
			   $(this).dialog("close");
               delData();
			  
            },
			'close': function() {
                $(this).dialog("close");
            }
        }
    });
}

function delCheck(){
  var stc_id=  $("input[name='stc_id[]']:checked").val();
	if(!stc_id){
	       alert('<{$lang.pls}><{$lang.unbind}><{$lang.customer}>!');
		   return;
	}else{
	   crmDelRowsTip("<{$lang.ok}><{$lang.unbind}><{$lang.customer}>-<{$lang.shipping_method}><{$lang.bind}>?");
	}
}

function delData(){
   var dataForm =$('#dataForm').serialize();
   var stc_id=  $("input[name='stc_id[]']:checked").val();
	if(!stc_id){
	       alert('<{$lang.pls_select}><{$lang.customer}>!');
		   return;
	}
	
   $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/shipping/ship-type-customer/batchdelete", //请求的处理url
        data: dataForm,
        success: function(json){
            var html ="";
            if(json.ask){
                alert("<{$lang.unbind}><{$lang.success}>!");
            }else{
                alert("<{$lang.unbind}><{$lang.fail}>!");
            }
            initData(0);
        }
    });
}


function del(stc_id){
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/shipping/ship-type-customer/delete", //请求的处理url
        data: {
		'stc_id':stc_id},
        success: function(json){
            var html ="";
            if(json.ask){
                alert("<{$lang.unbind}><{$lang.success}>!");
            }else{
                alert("<{$lang.unbind}><{$lang.fail}>!");
            }
            initData(0);
        }
    });
}

$(function(){
 $("[name=ckAll2]").click(function() {
        if ($(this).is(':checked')) {
            $(".stc_id0").attr('checked', true);
        } else {
            $(".stc_id0").attr('checked', false);
        }
    }); 
});

</script>
<style type="text/css">
.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}
</style>
<form id="dataForm" name="dataForm" >
<div id="iTitle">
	<div id="iTitleSub">
		<div>
		<label>
		<{$lang.shipping}><{$lang.code}>：
        <input  class="myInput" type="text" name="st_code" id="st_code">
		</label>&nbsp;
        <label>
		<{$lang.customer_code}>：
        <input  class="myInput" type="text" name="customer_id" id="customer_id">
		</label>&nbsp;
       
		<input class="myButton" type="button" value="<{$lang.search}>" onclick="search();">&nbsp;&nbsp;
		<input class="myButton" type="button" value="<{$lang.add}>"  id="addButton" onclick="showBindWindow()">&nbsp;&nbsp;
		<input class="myButton" type="button" value="<{$lang.delete}>"  id="delButton" onclick="delCheck()">&nbsp;&nbsp;
        <input  type="hidden" value="<{$count}>" name="count">&nbsp;&nbsp;
		</div>
	</div>
	<div id="tipmsg"></div>

</div>
<div class="listDiv ui-widget-content"></div>
<h3><{$lang.bind}><{$lang.shipping_method}><{$lang.list}></h3>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">	
    <thead>
    <tr>
	<th width="5%"><input type="checkbox" name="ckAll2" id="ckAll2"/></th>
	<th width="20%"><{$lang.bind}><{$lang.No}></th>
    <th width="20%"><{$lang.shipping}><{$lang.code}></th>
    <th width="20%"><{$lang.chinese}><{$lang.name}></th>
    <th width="20%"><{$lang.customer_code}></th>
    <th width="10%"><{$lang.delete}></th>            
    </tr>
    </thead>
	<tbody id="list"></tbody>
</table>
</form>
<div class="pagination"></div>
<div style="display:none;" class="messageDiv">
    <div id="message"></div>
</div>

<div style="display:none;" id="confrimDiv">
    <div id="confrimMsg"></div>
</div>

<div style="display:none;" id="bind" >
    <{include file='shipping/shiptypecustomer/bind.tpl'}>
</div>